//
//  DuanZiTableViewController.m
//  TT
//
//  Created by lanou3g on 15/6/6.
//  Copyright (c) 2015年 苏红召. All rights reserved.
//

#import "DuanZiTableViewController.h"
#import "DuanZiTableViewCell.h"
#import "DuanZi.h"
#import "DataHandle.h"
#import "MBProgressHUD+MJ.h"


static NSString *cellID = @"CellID";
@interface DuanZiTableViewController ()
{
    DataHandle *dataHandle;
}
@end

@implementation DuanZiTableViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.view.backgroundColor = [UIColor blackColor];
        
        self.automaticallyAdjustsScrollViewInsets = YES;
        UIBarButtonItem *right  = [[UIBarButtonItem alloc]initWithTitle:@"🔁" style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction1)];
        self.navigationItem.rightBarButtonItem = right;
    }
    return self;
}
 

- (void)viewDidAppear:(BOOL)animated{
    dataHandle = [DataHandle createDataHandle];
    
    [dataHandle addObserver:self forKeyPath:@"array" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    [self.tableView reloadData];
}


//button点击方法
-(void)buttonAction1{
    [MBProgressHUD showMessage:@"刷新中。。。。。"];
    dataHandle = [DataHandle createDataHandle];
    [self.tableView reloadData];
    
    [MBProgressHUD hideHUD];
  [MBProgressHUD showSuccess:[NSString stringWithFormat:@"为您刷新%@",dataHandle.tip]];
}


- (void)alertAction:(UIAlertView *)alert{
    [alert dismissWithClickedButtonIndex:0 animated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[DuanZiTableViewCell class] forCellReuseIdentifier:cellID];
    self.title = @"段子";
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return dataHandle.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DuanZi *d = dataHandle.array[indexPath.row];
    
    DuanZiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    self.tableView.rowHeight = [cell textHeight:d.content]+5;
    cell.backgroundColor = [UIColor lightTextColor];
    
    cell.duanzi = d;
    
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


// Override to support editing the table view.
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
//        
//    }   
//}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
